<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 随机订单号生成
 * @param string $pre  添加前缀，默认不添加
 * @param string $datatype  插入时间类型，默认 Ymd
 * @return string
 */
function randOrder($pre = null, $datatype = 'Ymd'){
    return $pre . date($datatype) . substr(implode(null, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}

/**
 * 
 * 产生随机字符串，不长于32位
 * @param int $length
 * @return 产生的随机字符串
 */
function getNonceStr($length = 32) 
{
	$chars = "abcdefghijklmnopqrstuvwxyz0123456789";  
	$str ="";
	for ( $i = 0; $i < $length; $i++ )  {  
		$str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
	} 
	return $str;
}

/**
 * 插入用户账单日志
 * @param int $uid  用户id
 * @param string $msg  内容
 * @param int $value  值
 */
function insertOperateLog($uid, $msg, $value){
	db('operate_log')->insert([
		'member_id'	=> $uid,
		'operate'	=> $msg,
		'value'		=> $value,
	]);
}

/**
 * 优惠券失效状态变更
 */
function expireCoup(){
	db('coupon')->where('coup_status', 0)
	->whereTime('coup_end_time', '<', date('Y-m-d'))
	->update(['coup_status' => 2]);
}

/**
 * 插入用户积分日志
 * @param string $operate  内容
 * @param int $value  值
 */
function insertPointLog($operate, $value){
	if (session('?user')){
		db('point_log')->insert([
			'member_id'	=> session('user.id'),
			'operate'	=> $operate,
			'value'		=> $value,
		]);
	}
}

/**
 * 获取毫秒级别的时间戳
 */
function getMillisecond()
{
	//获取毫秒的时间戳
	$time = explode ( " ", microtime () );
	$time = $time[1] . ($time[0] * 1000);
	$time2 = explode( ".", $time );
	$time = $time2[0];
	return $time;
}

/**
 * 将xml转为array
 * @param string $xml
 */
function FromXml($xml)
{	
	if(!$xml){
		throw new \Exception("xml数据异常！");
	}
	//将XML转为array
	//禁止引用外部xml实体
	libxml_disable_entity_loader(true);
	$res = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
	return $res;
}

/**
 * 导入订单通知队列
 * @param string $code  订单号
 * @param string $phone  订单联系人
 */
function saveOrderCache($code, $phone){
	$arr = cache('neworder');
	$arr[] = ['code' => $code, 'contact' => $phone,'time' => time()];
	cache('neworder', $arr);
}

/**
 * 导出订单通知队列
 * @param string $order  订单号
 */
 function pullTask($order){
	$arr = cache('neworder');
	foreach ($arr as $k => $v) {
		if($v['code'] == $order){
			\sms\Sendsms::sendMessage($v['contact'], config('sms.sign_name'), config('sms.tpl_code'), ['order'=>$order]);
			unset($arr[$k]);
			break;
		}
	}
	cache('neworder', $arr);
 }

/**
 * 把图片进行base编码
 * @param string $path  图片路径(初始路径在root_path/data/)
 */
function baseImage($path){
	$path = env('root_path').'data/'. $path;
	if($fp = fopen($path,"rb", 0)) 
	{ 
		$gambar = fread($fp,filesize($path)); 
		fclose($fp); 
		$base64 = chunk_split(base64_encode($gambar));
		return $base64;
	}
	return false;
}

/**
* 短信通知店主
* @param string $order  订单号
*/
function sendMessage($order){
	// \sms\Sendsms::sendMessage('15351217062', '长征众行', 'SMS_165060869', ['order'=>$order]);
}

/**
 * 获取访问ip
 */
function getUserIp(){
	$realip = "";
	if (isset($_SERVER)) {
		foreach(['HTTP_X_FORWARED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR'] as $name) {
			if (isset($_SERVER[$name])) {
				$realip = $_SERVER[$name];
				break;
			}
		}
	} else {
		$realip = getenv('HTTP_X_FORWARDED_FOR') || getenv('HTTP_CLIENT_IP') || getenv('REMOTE_ADDR');
	}
	return $realip;
}

/**
 * 判断是否通过域名访问
 */
function isDomain(){
	$host = request()->host();
}